//
//  ProductItemDetailViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15-5-19.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "ProductItemDetailViewController.h"
#import "ItemDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
#import <CoreLocation/CoreLocation.h>
#import "SearchMemberTableViewCell.h"
//#import "FPNumberPadView.h"

#import "UpYun.h"

#import "CaptureViewController.h"
#import "PassImageDelegate.h"
//#import "ProductDetailViewController.h"



@interface ProductItemDetailViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,PassImageDelegate>
{
   // ProductDetailViewController *productDetailVC;
   // NSMutableArray *mTagsArray;
    NSMutableArray *mImagesArray;
   // NSMutableArray *imageHeightArray;
   // NSMutableArray *tagNameArray;
    NSUInteger _memberPageIndex;
    NSUInteger _memberTotalCount;
    NSUInteger _memberTotalPage;
    
    NSMutableArray *memberCodeArray;
    NSMutableArray *memberNameArray;
    NSMutableArray *memberPhoneArray;
    NSMutableArray *memberGradeArray;
    NSMutableArray *memberAddressArray;
    NSMutableArray *memberIntegralArray;
    NSMutableArray *memberSexArray;

    NSString *weightStr;
    NSString *cutGoldStr;
    NSString *processPriceStr;
    NSString *todayGoldPriceStr;
    
    NSInteger priceDetailCellTag;
    BOOL isOpened;
   
    NSMutableArray *productImageArray;
    
     NSMutableArray *upYunImageArray;
    
    NSString *policyStr;
    NSString *signatureStr;
    
    NSString *imageURL;
    
    NSInteger leftTableTag;
    
    NSString *downLoadUrlStr;
    BOOL isLoad;
    
    NSString *detailTypeStr;

}

@end

@implementation ProductItemDetailViewController


-(void)viewWillAppear:(BOOL)animated
{
    if (isLoad==NO) {
        
   
    [Global setShopCartID:3];
    [Global setOrderID:3];
    
    [self initCameraViewContainer];
   // NSLog(@"_itemmmmURL======%@",_itemUrlArray);
   // mTagsArray=[NSMutableArray array];
    productImageArray=[NSMutableArray array];
    imageURL=@"";
    downLoadUrlStr=@"";
//    _productImagePageControl.numberOfPages = 0;
//    _productImagePageControl.currentPageIndicatorTintColor = [UIColor blackColor];
//    _productImagePageControl.pageIndicatorTintColor = [UIColor grayColor];
    _addToShopCartBtn.layer.borderColor=[[UIColor colorWithRed:210.0f/255.0f green:171.0f/255.0f blue:57.0f/255.0f alpha:1] CGColor];
    _addToShopCartBtn.layer.borderWidth= 1.0f;
    _addToShopCartBtn.layer.cornerRadius=3;

     [_shopCartBtn addTarget:self action:@selector(shopCartBtnClick) forControlEvents:UIControlEventTouchUpInside];
   // imageHeightArray=[NSMutableArray array];
    mImagesArray=[NSMutableArray array];
    [_rightScrollView setScrollEnabled:YES];
    [_rightScrollView setContentOffset:CGPointMake(0, 0)];
            detailTypeStr=@"Product/ItemDetail";
    if ([Global getHomeCustomID]==20||[Global getHomeCustomID]==35) {
        leftTableTag=_selectID;
        if ([Global getHomeCustomID]==20) {
            _shopCartBtn.hidden=NO;
            _upImageNoteLabel.hidden=NO;
            [_upImageButton setUserInteractionEnabled:YES];
            [_productImageBtn setUserInteractionEnabled:YES];
            //[_addToShopCartBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        }
        else if ([Global getHomeCustomID]==35)
        {
            
            detailTypeStr=@"Manufacturer/PlatFormItemDetail";
            _upImageNoteLabel.hidden=YES;
            [_upImageButton setUserInteractionEnabled:NO];
            [_productImageBtn setUserInteractionEnabled:NO];
            _shopCartBtn.hidden=YES;
           // [_addToShopCartBtn setTitle:@"加入代销" forState:UIControlStateNormal];

        }
    
    if (_itemUrlArray.count>0) {
        if ([_itemUrlArray objectAtIndex:_selectID]!=[NSNull null]) {
             [ _productImage setImageWithURL:[NSURL URLWithString:[_itemUrlArray objectAtIndex:_selectID]]];
        }
       
    }
    if (_itemTypeNameArray.count>0) {
         if ([_itemTypeNameArray objectAtIndex:_selectID]!=[NSNull null])
             {
        _styleNameLabel.text=[_itemTypeNameArray objectAtIndex:_selectID];
             }
    }
//    if (_itemSeriesNameArray.count>0) {
//        if ([_itemSeriesNameArray objectAtIndex:_selectID]!=[NSNull null]) {
//            
//        _seriesNameLabel.text=[_itemSeriesNameArray objectAtIndex:_selectID];
//        }
//    }
    if (_itemInDateArray.count>0) {
        if ([_itemInDateArray objectAtIndex:_selectID]!=[NSNull null])
        {
        _dateLabel.text=[_itemInDateArray objectAtIndex:_selectID];
    }
    }
    if (_itemWeightArray.count>0) {
         if ([_itemWeightArray objectAtIndex:_selectID]!=[NSNull null])
         {
        _weightLabel.text=[NSString stringWithFormat:@"%0.02fg",[[_itemWeightArray objectAtIndex:_selectID] floatValue]];
         }
    }
       if (_itemPriceArray.count>0) {
      //  _priceLabel.text=[NSString stringWithFormat:@"¥%0.02f",[[_itemPriceArray objectAtIndex:_selectID] floatValue]];
           if ([_itemPriceArray objectAtIndex:_selectID]!=[NSNull null])
           {
           _priceLabel.text= [NSString stringWithFormat:@"¥%@",[Global floatConvertPercentile:[[_itemPriceArray objectAtIndex:_selectID] floatValue]]];
       }
    }
    if(_itemWhereArray.count>0)
    
    {
         if ([_itemWhereArray objectAtIndex:_selectID]!=[NSNull null])
         {
       _addressLabel.text=[_itemWhereArray objectAtIndex:_selectID];
    }
    }
       if (_itemNameArray.count>0) {
           if ([_itemNameArray objectAtIndex:_selectID]!=[NSNull null])
           {
        _productNameLabel.text=[NSString stringWithFormat:@"%@ %@",[_itemCodeArray objectAtIndex:_selectID],[_itemNameArray objectAtIndex:_selectID]];
           }
    }
    [_leftTableView reloadData];
    //  [_leftTableView setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:241.0f/255.0f alpha:1.0f]];
        
      //  NSLog(@"_selectID====%d",_selectID);
    [_leftTableView setBackgroundColor:[UIColor clearColor]];
    if (_itemCodeArray.count>0) {
        NSIndexPath *ip= [NSIndexPath indexPathForRow:_selectID inSection:0];
        [_leftTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
    }
    }

    if ([Global getHomeCustomID]==10) {
        leftTableTag=0;
        if (_itemUrlArray!=nil) {
            [_itemUrlArray removeAllObjects];
        }
        if (_itemTypeNameArray!=nil) {
            [_itemTypeNameArray removeAllObjects];
        }
        if (_itemSeriesNameArray!=nil) {
            [_itemSeriesNameArray removeAllObjects];
        }
        if (_itemInDateArray!=nil) {
            [_itemInDateArray removeAllObjects];
        }

        if (_itemWeightArray!=nil) {
            [_itemWeightArray removeAllObjects];
        }
        if (_itemPriceArray!=nil) {
            [_itemPriceArray removeAllObjects];
        }
        if (_itemWhereArray!=nil) {
            [_itemWhereArray removeAllObjects];
        }

        if (_itemNameArray!=nil) {
            [_itemNameArray removeAllObjects];
        }

        if (_itemCodeArray!=nil) {
            [_itemCodeArray removeAllObjects];
        }
       [_leftTableView setBackgroundColor:[UIColor clearColor]];
       [_leftTableView reloadData];

    }
        
        for (UIView *v in _rightViewContainer.subviews) {
            if (v.tag==9) {
                
                [v removeFromSuperview];
            }
            
        }
    for (UIView *v in _rightScrollView.subviews) {
        if (v.tag==9) {
            
            [v removeFromSuperview];
        }
        
    }
//    for (UIView *v in _productImageScrollView.subviews) {
//        [v removeFromSuperview];
//    }
       [self requestItemDetail:_itemCodeStr];
    }
}
-(void)initCameraViewContainer
{
    policyStr=@"";
    signatureStr=@"";
    _productImageBtn.hidden=YES;
    _upImageButton.hidden=NO;
    CGRect frame=_rightViewContainer.frame;
    frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
    _rightViewContainer.frame=frame;

    CGRect viewFrame1=_photoPickerViewContainer.frame;
    viewFrame1.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame1;
    
     upYunImageArray=[NSMutableArray array];
    
     _upImageButton.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1] CGColor];
    _upImageButton.layer.borderWidth= 1.0f;

   [_productImageBtn addTarget:self action:@selector(upImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
   [_upImageButton addTarget:self action:@selector(upImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)upImageBtnClick:(id)sender
{
    
    [UIView animateWithDuration:0.5f animations:^
     {
         CGRect viewFrame=_photoPickerViewContainer.frame;
         viewFrame.origin.y=0;
         _photoPickerViewContainer.frame=viewFrame;
     }completion:^(BOOL finished) {

         
     }];
//    [_CameraHelper changePreviewOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
//
}
#pragma 拍照选择照片协议方法
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    
//}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     isLoad=YES;
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    NSData *data;
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        //切忌不可直接使用originImage，因为这是没有经过格式化的图片数据，可能会导致选择的图片颠倒或是失真等现象的发生，从UIImagePickerControllerOriginalImage中的Origin可以看出，很原始，哈哈
        UIImage *originImage = [info objectForKey:UIImagePickerControllerOriginalImage];
       // NSLog(@"originImage.",originImage.size);
        //图片压缩，因为原图都是很大的，不必要传原图
        UIImage *scaleImage = [self scaleImage:originImage toScale:0.7];
        
        //以下这两步都是比较耗时的操作，最好开一个HUD提示用户，这样体验会好些，不至于阻塞界面
        if (UIImagePNGRepresentation(scaleImage) == nil) {
            //将图片转换为JPG格式的二进制数据
            data = UIImageJPEGRepresentation(scaleImage, 1);
        } else {
            //将图片转换为PNG格式的二进制数据
            data = UIImagePNGRepresentation(scaleImage);
        }
        
        //将二进制数据生成UIImage
        UIImage *image = [UIImage imageWithData:data];
        
        //将图片传递给截取界面进行截取并设置回调方法（协议）
        CaptureViewController *captureView = [[CaptureViewController alloc] init];
        captureView.delegate = self;
        captureView.image = image;
        //隐藏UIImagePickerController本身的导航栏
        picker.navigationBar.hidden = NO;
        [picker pushViewController:captureView animated:YES];
        
    }
}

#pragma mark - 图片回传协议方法
-(void)passImage:(UIImage *)image
{
    _productImageBtn.hidden=NO;
    _upImageButton.hidden=YES;
    _productImage.image = image;
    
   // isLoad=NO;
    
    CGRect frame=_rightViewContainer.frame;
    frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
    _rightViewContainer.frame=frame;
    
    [upYunImageArray removeAllObjects];
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:@"Product" forKey:@"uploadType"];
        // NSLog(@"params====%@",params);
        [HttpRequest requestLoginData:@"System/GetFormParams" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
          //   NSLog(@"upyunParams==%@",severDataDict.dataDict);
            // [_cashierSearchMemberTextField resignFirstResponder];
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    [Global setUpYunUrl:[severDataDict.dataDict objectForKey:@"action"]];
                    signatureStr=[severDataDict.dataDict objectForKey:@"signature"];
                    policyStr=[severDataDict.dataDict objectForKey:@"policy"];
                    
                    [Global setSignatureStr:signatureStr];
                    [Global setPolicyStr:policyStr];
                    
                    downLoadUrlStr=[severDataDict.dataDict objectForKey:@"ImageZone"];
                    
                    [self upToUpYun];
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    if(imageURL.length>0)
                    {
                        [_productImage setImageWithURL:[NSURL URLWithString:imageURL]];
                        
                        _productImageBtn.hidden=NO;
                        
                        _upImageButton.hidden=YES;
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                    }
                    else
                    {
                        _productImageBtn.hidden=YES;
                        _productImage.image =nil;
                        _upImageButton.hidden=NO;
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                        
                    }
 
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    [Global loginViewController:self noteStr:requestResultStr];
                      //  [Global loginViewController:self];
                    }

                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }

}

#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

-(IBAction)albumOrCameraBtnClick:(UIButton *)sender
{
    CGRect viewFrame=_photoPickerViewContainer.frame;
    viewFrame.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame;
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
       switch (sender.tag) {
        case 20000://Take picture
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                
            }else{
                NSLog(@"模拟器无法打开相机");
            }
             [self presentViewController:picker animated:YES completion:nil];
            break;
            
        case 20001://From album
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
             [self presentViewController:picker animated:YES completion:nil];
            break;
            
        default:
            
            break;
    }
   
}

-(IBAction)cancelBtnClick:(id)sender
{
    // _CameraHelper.selectOrCancel=@"CANCEL";
    CGRect viewFrame=_photoPickerViewContainer.frame;
    viewFrame.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame;
  // [_CameraHelper stopRunning];
   

}
-(void)upToUpYun
{
     isLoad=NO;
    __block NSInteger count=0;
    UpYun *uy = [[UpYun alloc] init];
    uy.successBlocker = ^(id data)
    {
        //  NSLog(@"zuzhijigouDATA======%@",data );
        count++;
        // NSLog(@"count======%d",count );
        // NSLog(@"_pictureView.subviews.count======%d",_pictureView.subviews.count );
        [Global hideProgressHUDInternal:self.view];
        NSString *imageStr;
        NSString *imageWIDTH;
        NSString *imageHEIGHT;
        imageStr=[data objectForKey:@"url"];
        NSString *imgUrl=[NSString stringWithFormat:@"/%@",imageStr];
        imageWIDTH=[data objectForKey:@"image-width"];
        imageHEIGHT=[data objectForKey:@"image-height"];
        NSMutableDictionary *upYunImageDict=[NSMutableDictionary dictionary];
        [upYunImageDict setObject:imgUrl forKey:@"ImageUrl"];
        [upYunImageDict setObject:imageWIDTH forKey:@"ImageWidth"];
        [upYunImageDict setObject:imageHEIGHT forKey:@"ImageHeigh"];
         [upYunImageArray addObject:upYunImageDict];
       
        if ([Global getLoginToken].length!=0&& upYunImageDict.count>0) {
            [Global showIndeterminateHudSub:@"上传中，请稍后。。。" delay:30 view:self.view];
            
            NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
            [params setObject:[Global getLoginToken] forKey:@"Token"];
            
             [params setObject:_itemCodeStr forKey:@"ItemCode"];
            
            [params setObject:upYunImageArray forKey:@"DataLine"];
          //  NSLog(@"params===%@",params);
            [HttpRequest requestData:@"Product/UpdateItemImg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                // NSLog(@"uploadSever===%@",severDataDict.dataDict);
                // NSLog(@"uploadSeverRESULTSTR===%@",severDataDict.resultStr);
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        
                        [Global showTextHudSub:requestResultStr view:self.view];
                        // NSLog(@"touxiang====%@",severDataDict.dataDict);
                      //  [_uploadBtn setTitle:@"已上传" forState:UIControlStateNormal];
                        // [self uploadImage];
                        _productImageBtn.hidden=NO;
                        
                        _upImageButton.hidden=YES;
                        
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                        NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:leftTableTag inSection:0];
                        UITableViewCell *cell = [_leftTableView cellForRowAtIndexPath:indexPath1];
                         ItemDetailTableViewCell *leftCell=( ItemDetailTableViewCell *)cell;
                        
                        NSString *imgStr=[NSString stringWithFormat:@"%@%@",downLoadUrlStr,imgUrl];
                       // NSLog(@"imgStr=====%@",imgStr);
                        
                        [ leftCell.productImageView setImageWithURL:[NSURL URLWithString:imgStr]];
                        [_itemUrlArray replaceObjectAtIndex:leftTableTag withObject:imgStr];

                    }
                    else
                    {
                        
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                       // [_uploadBtn setTitle:@"上传失败" forState:UIControlStateNormal];
                        // [Global showTextHud:requestResultStr view:self.view];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        [Global loginViewController:self noteStr:requestResultStr];
                          //  [Global loginViewController:self];
                        }

                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                  //  [_uploadBtn setTitle:@"上传失败" forState:UIControlStateNormal];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
            }];
        }
        
        
        
        
    };
    uy.failBlocker = ^(NSError * error)
    {
        [Global hideProgressHUDInternal:self.view];
        
     
        if(imageURL.length>0)
        {
            [_productImage setImageWithURL:[NSURL URLWithString:imageURL]];
            
            _productImageBtn.hidden=NO;
           
            _upImageButton.hidden=YES;
            
            CGRect frame=_rightViewContainer.frame;
            frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
            _rightViewContainer.frame=frame;

        }
        else
        {
             _productImageBtn.hidden=YES;
            _productImage.image =nil;
            _upImageButton.hidden=NO;
            
            CGRect frame=_rightViewContainer.frame;
            frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
            _rightViewContainer.frame=frame;
        
            
        }
        
            NSString *message = [error.userInfo objectForKey:@"message"];
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"error" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        //  NSLog(@"%@",error);
    };
    uy.progressBlocker = ^(CGFloat percent, long long requestDidSendBytes)
    {
        // [_pv setProgress:0.3333333333+percent/3];
    };
    // NSLog(@"selectePictureArray===%@",selectePictureArray);
    //for (int i=0; i<selectePictureArray.count; i++) {
    // UIImageView *imageview=(UIImageView *)[selectePictureArray  objectAtIndex:i];
    
    if (_productImage.image!=nil) {
        UIImage *img=[self imageCompress:_productImage.image];
        
        _productImageBtn.hidden=NO;
      
        _upImageButton.hidden=YES;
        
        
        CGRect frame=_rightViewContainer.frame;
        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
        _rightViewContainer.frame=frame;

        
        [Global showIndeterminateHudSub:@"正上传UPYUN，请稍后。。。" delay:120 view:self.view];
       uy.bucket=@"";
        [uy uploadFile:img saveKey:[self getSaveKey]];
        
            }
    //    }
        
 
}

-(NSString * )getSaveKey {
    NSDate *d = [NSDate date];
    int value = arc4random() % 1000;
    return [NSString stringWithFormat:@"/%d/%d/%d.jpg",[self getYear:d],[self getMonth:d],value];
}

- (int)getYear:(NSDate *) date{
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSYearCalendarUnit;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    int year=[comps year];
    return year;
}

- (int)getMonth:(NSDate *) date{
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    int month = [comps month];
    return month;
}
-(UIImage *)imageCompress:(UIImage *)image
{
    UIImage *comImage=image;
    NSData * sendImageData = UIImageJPEGRepresentation(comImage, 1.0);
    NSUInteger sizeOrigin = [sendImageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    
    if (sizeOriginKB >200) {
        float a = 200.0000;
        float b = (float)sizeOriginKB;
        float q = sqrtf(a / b);
        
        CGSize sizeImage = [comImage size];
        CGFloat widthSmall = sizeImage.width * q;
        CGFloat heighSmall = sizeImage.height * q;
        CGSize sizeImageSmall = CGSizeMake(widthSmall, heighSmall);
        
        UIGraphicsBeginImageContext(sizeImageSmall);
        CGRect smallImageRect = CGRectMake(0, 0, sizeImageSmall.width, sizeImageSmall.height);
        [image drawInRect:smallImageRect];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        NSData *dataImage = UIImageJPEGRepresentation(smallImage, 1.0);
        sendImageData = dataImage;
        
        comImage= smallImage;
        // [_imageview1.image retain];
    }
    return comImage;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)requestItemDetail:(NSString *)codeStr
{
    _styleNameLabel.text=@"";
    _seriesNameLabel.text=@"";
    _priceLabel.text=@"";
    _dateLabel.text=@"";
    _addressLabel.text=@"";
    _weightLabel.text=@"";
    _certIdLabel.text=@"";
    _securitycodeLabel.text=@"";
    _mainQtyLabel.text=@"";
    _adverbNameLabel.text=@"";
    _neatNessNameLabel.text=@"";
    _shapeLabel.text=@"";
    _adverbQtyNameLabel.text=@"";
    _gramLabel.text=@"";
    _itemCodeLabel.text=@"";
    _seriesCodeLabel.text=@"";
    _typeNameLabel.text=@"";
    _designerLabel.text=@"";
    _cadDateLabel.text=@"";
    _itypeNameLabel.text=@"";
    _productNameLabel.text=@"";
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:codeStr forKey:@"ItemCode"];
        if ([Global getHomeCustomID]==35) {
            [params setObject:_companyCodeStr forKey:@"CompanyCode"];
        }
        [HttpRequest requestData:detailTypeStr params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                
              //  NSLog(@"detailTypeStr====%@",detailTypeStr);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                //  NSLog(@"padResult=%@",severDataDict.dataDict);
                    [Global hideProgressHUDInternal:self.view];
                   // [_productImage setImageWithURL:[NSURL URLWithString:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ImageUrl"]]];
                    [productImageArray removeAllObjects];
//                    for (UIView *v in _productImageScrollView.subviews) {
//                        [v removeFromSuperview];
//                    }
                    if ([Global getHomeCustomID]==10) {
                       
                        _itemUrlArray=[NSMutableArray array];
                        _itemTypeNameArray=[NSMutableArray array];
                        _itemSeriesNameArray=[NSMutableArray array];
                        _itemInDateArray=[NSMutableArray array];
                        _itemWeightArray=[NSMutableArray array];
                        _itemPriceArray=[NSMutableArray array];
                        _itemWhereArray=[NSMutableArray array];
                        _itemNameArray=[NSMutableArray array];
                        _itemCodeArray=[NSMutableArray array];
                        [_itemUrlArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ImageUrl"]];
                        [_itemTypeNameArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItypeName"]];
                        [_itemSeriesNameArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"SeriesName"]];
                        [_itemInDateArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"InDate"]];
                        [_itemWeightArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Gram"]];
                        [_itemPriceArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Price"]];
                        [_itemWhereArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"WhsName"]];
                        [_itemNameArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemName"]];
                        [_itemCodeArray addObject:[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemCode"]];
                        [_leftTableView reloadData];
                        if (_itemCodeArray.count>0) {
                            NSIndexPath *ip= [NSIndexPath indexPathForRow:0 inSection:0];
                            [_leftTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
                        }

                    }
                    NSMutableArray *productImgArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataItemImgs"]];
                  //  NSLog(@"productImgArray===%@",productImgArray);
                    if (productImgArray.count>0) {
                        _productImageBtn.hidden=NO;
                        _upImageButton.hidden=YES;
                        [_productImage setImageWithURL:[NSURL URLWithString:[[productImgArray objectAtIndex:0] objectForKey:@"ImageUrl"]]];
                        imageURL=[[productImgArray objectAtIndex:0] objectForKey:@"ImageUrl"];
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                       //NSLog(@"productImgArray====%@",productImgArray);
                    }
                    else
                    {
                        _productImageBtn.hidden=YES;
                        _upImageButton.hidden=NO;
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                      //   NSLog(@"frame.origin.y111====%f",frame.origin.y);

                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItypeName"]!=[NSNull null]) {
                        _styleNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItypeName"];
                        
                        _styleNameLabel.numberOfLines = 0;
                        _styleNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000, _styleNameLabel.frame.size.height);
                        
                        CGSize expectSize = [_styleNameLabel sizeThatFits:maximumLabelSize];
                        CGRect frame1 = CGRectMake(_styleNameLabel.frame.origin.x, _styleNameLabel.frame.origin.y,expectSize.width, _styleNameLabel.frame.size.height );
                        _styleNameLabel.frame=frame1;

                   
                   // [self resizeLabel:_styleNameLabel];
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ISectypeName"]!=[NSNull null]) {
                     _seriesNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ISectypeName"];
                        
                        _seriesNameLabel.numberOfLines = 0;
                        _seriesNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000, _seriesNameLabel.frame.size.height);
                        
                        CGSize expectSize = [_seriesNameLabel sizeThatFits:maximumLabelSize];
                        CGRect frame1 = CGRectMake(_seriesNameLabel.frame.origin.x,_seriesNameLabel.frame.origin.y,expectSize.width,_seriesNameLabel.frame.size.height );
                        _seriesNameLabel.frame=frame1;

                    
                    // [self resizeLabel:_seriesNameLabel];
                    CGRect frame0=_seriesNameLabel.frame;
                    frame0.origin.x=_styleNameLabel.frame.origin.x+_styleNameLabel.frame.size.width+3;
                    _seriesNameLabel.frame=frame0;
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Price"]!=[NSNull null]) {
                     _priceLabel.text=[NSString stringWithFormat:@"¥%@",[Global floatConvertPercentile:[[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Price"] floatValue]]];
                  
                  //  [self resizeLabel:_priceLabel];
                      _priceLabel.numberOfLines = 0;
                      _priceLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000,_priceLabel.frame.size.height);
                        
                        CGSize expectSize = [_priceLabel sizeThatFits:maximumLabelSize];
                        CGRect frame0 = CGRectMake(_priceLabel.frame.origin.x, _priceLabel.frame.origin.y,expectSize.width, _priceLabel.frame.size.height );
                      _priceLabel.frame=frame0;

                    
                    CGRect frame1=_weightTagLabel.frame;
                    frame1.origin.x=_priceLabel.frame.origin.x+_priceLabel.frame.size.width+10;
                    _weightTagLabel.frame=frame1;
                    
                    CGRect frame2=_weightLabel.frame;
                    frame2.origin.x=_weightTagLabel.frame.origin.x+_weightTagLabel.frame.size.width+3;
                    _weightLabel.frame=frame2;
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemName"]!=[NSNull null]) {
                        _productNameLabel.text=[NSString stringWithFormat:@"%@ %@",[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemCode"],[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemName"]];
                    }

                     if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"InDate"]!=[NSNull null]) {
                    _dateLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"InDate"];
                     }
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"WhsName"]!=[NSNull null]) {

                     _addressLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"WhsName"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]!=[NSNull null]) {
                     _weightLabel.text=[NSString stringWithFormat:@"%@g",[Global floatConvertPercentile:[[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"] floatValue]]];
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"CertId"]!=[NSNull null]) {
                   _certIdLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"CertId"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"MainQty"]!=[NSNull null]) {
                        _mainQtyLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"MainQty"];
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Size"]!=[NSNull null]) {
                        _sizeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Size"];
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Securitycode"]!=[NSNull null]) {
                         _securitycodeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Securitycode"];
                     
                     }
                     if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbName"]!=[NSNull null]) {
                   _adverbNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbName"];
                     }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"NeatNess"]!=[NSNull null]) {
                   
                    _neatNessNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"NeatNess"];
                    }
                    
                     if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Shape"]!=[NSNull null]) {
                    _shapeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Shape"];
                     }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbQty"]!=[NSNull null]) {
                         _adverbQtyNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbQty"];
                    }
                 
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]!=[NSNull null]) {
                    _gramLabel.text=[NSString stringWithFormat:@"%@g",[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]];
                    }
                     if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemCode"]!=[NSNull null])
                     {
                    _itemCodeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ArtNo"];
                     }
                    NSString *productTextViewStr=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Introduction"];
                    _productTextView.text=productTextViewStr;
                    CGFloat height=  [Global heightForTextView:_productTextView WithText:_productTextView.text];
                    
                    CGRect frameP=_productTextView.frame;
                    frameP.size.height=height;
                    _productTextView.frame=frameP;
                    
                    NSString *seriesCodeStr=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesCode"];
                    if (seriesCodeStr.length>0) {
                        _titleLabel.text=@"款式详情";
                       // _detailNameLabel.text=@"款式详情";
                        _seriesView.hidden=NO;
                        

                        CGRect frameA=_seriesView.frame;
                        frameA.origin.y=_productTextView.frame.origin.y+_productTextView.frame.size.height+5;
                        _seriesView.frame=frameA;
                        
                        CGRect frameB=_detailLabelView.frame;
                        frameB.origin.y=_seriesView.frame.origin.y+_seriesView.frame.size.height+5;
                        _detailLabelView.frame=frameB;
                        
                         _seriesCodeLabel.text=seriesCodeStr;
                  
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesName"]!=[NSNull null])
                    {
                    _typeNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesName"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Designer"]!=[NSNull null])
                    {
                    _designerLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Designer"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"CadDate"]!=[NSNull null])
                    {

                    _cadDateLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"CadDate"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"ItypeName"]!=[NSNull null])
                    {
                    _itypeNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"ItypeName"];
                    }
                   NSString *seriesViewStr=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Introduction"];
                    _seriesTextView.text=seriesViewStr;
                    CGFloat height=  [Global heightForTextView:_seriesTextView WithText:_seriesTextView.text];
                        
                    CGRect frameP=_seriesTextView.frame;
                    frameP.size.height=height;
                    _seriesTextView.frame=frameP;
                        
                   NSMutableArray *mTagsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLineLabel"]];
                     //   NSLog(@"mTagsArray===%@",mTagsArray);
                     float height1=0.0;
                    for (int i=0; i<mTagsArray.count; i++) {
                        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(83, 0, 85, 32)];
                        [label setFont:[UIFont fontWithName:@"fontWithName" size:15]];
                        [label setText:[[mTagsArray objectAtIndex:i] objectForKey:@"LabelName"]];
                      //  [self resizeLabel:label];
                        [Global resizeForLable:label WithText:label.text parentView:self.view];
                        CGRect rect=label.frame;
                        rect.size.height=32;
                        rect.size.width=label.frame.size.width;
                        rect.origin.x=86+(label.frame.size.width+15)*(i%6);
                        rect.origin.y=46*(i/6);
                        label.frame=rect;
                        label.tag=9;
                          [label setTextColor:[UIColor colorWithRed:209.0f/255.0f green:174.0f/255.0f blue:80.0f/255.0f alpha:1.0f] ];
                        [_seriesTagView addSubview:label];

                    }
                          height1=height1+(mTagsArray.count/6+1)*35;
                        CGRect frameT=_seriesTagView.frame;
                        frameT.size.height=height1;
                        _seriesTagView.frame=frameT;
                        
                        CGRect frameS=_seriesView.frame;
                        frameS.size.height=_seriesTagView.frame.size.height+_seriesTagView.frame.origin.y;
                        _seriesView.frame=frameS;

//                    }
                   
                }
                else
                {
                     _titleLabel.text=@"产品详情";
                    // _detailNameLabel.text=@"产品详情";
                    _seriesView.hidden=YES;
//                    CGRect frameA=_seriesView.frame;
//                    frameA.origin.y=_productTextView.frame.origin.y+_productTextView.frame.size.height+5;
//                    _seriesView.frame=frameA;
                    
                    CGRect frameB=_detailLabelView.frame;
                    frameB.origin.y=_productTextView.frame.origin.y+_productTextView.frame.size.height+5;
                    _detailLabelView.frame=frameB;

                }
                
                //webview
                    NSString *urlStr=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemDespUrl"];
                   
                    CGRect rect = _itemUrlWebView.frame;
                    rect.origin.y=_detailLabelView.frame.origin.y+_detailLabelView.frame.size.height+10;
                    rect.size.height=1;
                    
                    _itemUrlWebView.frame=rect;
                    _itemUrlWebView.scrollView.bounces = NO;
                    [_itemUrlWebView setScalesPageToFit:NO];
                    _itemUrlWebView.scrollView.showsHorizontalScrollIndicator = NO;
                    _itemUrlWebView.scrollView.scrollEnabled = NO;
                   // [_itemUrlWebView scalesPageToFit];
                   // NSLog(@"urlStr===%@",urlStr);
                  
                    if (urlStr.length>0) {
                        
                 // [_itemUrlWebView setPaginationMode:UIWebPaginationModeTopToBottom];
                    NSURL *url = [[NSURL alloc]initWithString:urlStr];
                 [_itemUrlWebView loadRequest:[NSURLRequest requestWithURL:url]];
                        
//                        CGRect rect1 = _rightViewContainer.frame;
//                        rect1.size.height=_itemUrlWebView.frame.origin.y+_itemUrlWebView.frame.size.height+10;
//                        _rightViewContainer.frame=rect1;

                    
                }
//                else
//                {
//                    [_rightScrollView setScrollEnabled:YES];
//                    
//                    CGRect rect1 = _rightViewContainer.frame;
//                    rect1.size.height=_cadDateLabel.frame.origin.y+_cadDateLabel.frame.size.height+10;
//                    _rightViewContainer.frame=rect1;
//
//                    
//                    [_rightScrollView setContentSize:CGSizeMake(0, _rightViewContainer.frame.origin.y+_rightViewContainer.frame.size.height+height1+10)];
//                    //  NSLog(@"frame gggg= %f",htmlHeight);
//                    [_rightScrollView setShowsVerticalScrollIndicator:NO];
//                }
            }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        [Global loginViewController:self noteStr:requestResultStr];
                     //   [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (webView==_itemUrlWebView) {
        
        [self performSelector:@selector(getWebHeight) withObject:nil afterDelay:0.3];
        
    }
}
-(void)getWebHeight
{
   // [_itemUrlWebView sizeToFit];
    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", _itemUrlWebView.frame.size.width];
    [_itemUrlWebView stringByEvaluatingJavaScriptFromString:meta];


    CGSize actualSize = [_itemUrlWebView sizeThatFits:CGSizeZero];
    CGRect newFrame = _itemUrlWebView.frame;
    newFrame.size.height = actualSize.height;
    _itemUrlWebView.frame = newFrame;
    
  //  CGSize newsize=CGSizeMake(620, 356+_itemUrlWebView.frame.size.height);
    //_itemUrlWebView.contentSize=newsize;
   
    
    
    CGFloat height = _itemUrlWebView.scrollView.contentSize.height;
   // NSLog(@"height===%f",height);
    
    
    CGRect rect1 = _rightViewContainer.frame;
    rect1.size.height=_itemUrlWebView.frame.origin.y+height;
    _rightViewContainer.frame=rect1;
    
    [_rightScrollView setScrollEnabled:YES];
  
    [_rightScrollView setContentSize:CGSizeMake(0, _rightViewContainer.frame.origin.y+_rightViewContainer.frame.size.height)];
    //  NSLog(@"frame gggg= %f",htmlHeight);
    [_rightScrollView setShowsVerticalScrollIndicator:NO];
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==_leftTableView) {
  
        return _itemCodeArray.count;
    }
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_leftTableView) {
        
    return 120;
    }
    else
        return 50;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==_leftTableView) {
    
        static NSString * const  leftIdentifier = @"leftIdentifier";
        ItemDetailTableViewCell *cell=( ItemDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier: leftIdentifier];
        if (cell==nil) {
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ItemDetailTableViewCell" owner:self options:nil];
            cell = [array objectAtIndex:0];
        }
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame] ;
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
//     NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:_selectID inSection:indexPath.section];
//       // if (indexPath.row==0) {
//            [tableView selectRowAtIndexPath:indexPath1 animated:NO scrollPosition:UITableViewScrollPositionNone];
       // }
    if (_itemUrlArray.count>0) {
        if ([_itemUrlArray objectAtIndex:indexPath.row]!=[NSNull null]&&[[_itemUrlArray objectAtIndex:indexPath.row] length]>0) {
        [ cell.productImageView setImageWithURL:[NSURL URLWithString:[_itemUrlArray objectAtIndex:indexPath.row]]];
        }
        else
        {
        [cell.productImageView setImage:[UIImage imageNamed:@"zhanweitu_.png"]];
        }
    }
    if (_itemNameArray.count>0) {
        if ([_itemNameArray objectAtIndex:indexPath.row]!=[NSNull null]) {
        cell.productNameLabel.text=[NSString stringWithFormat:@"%@ %@",[_itemCodeArray objectAtIndex:indexPath.row],[_itemNameArray objectAtIndex:indexPath.row]];//[_itemNameArray objectAtIndex:indexPath.row];
        }
    }
    if (_itemPriceArray.count>0) {
         if ([_itemPriceArray objectAtIndex:indexPath.row]!=[NSNull null]) {
        cell.productPriceLabel.text=[NSString stringWithFormat:@"¥%@",[Global floatConvertPercentile:[[_itemPriceArray objectAtIndex:indexPath.row] floatValue]]];
         }
    }
//    if(_itemWhereArray.count>0)
//        
//    {
//        cell.stockAddressLabel.text=[_itemWhereArray objectAtIndex:indexPath.row];
//    }
    if (_itemWeightArray.count>0) {
        if ([_itemWeightArray objectAtIndex:indexPath.row]!=[NSNull null]) {
        cell.productWeightLabel.text=[NSString stringWithFormat:@"%0.02fg",[[_itemWeightArray objectAtIndex:indexPath.row] floatValue]];
          }
        [Global resizeForLable: cell.productWeightLabel WithText: cell.productWeightLabel.text parentView:self.view];
      //  [self resizeLabel: cell.productWeightLabel];
        CGRect frame=cell.productPriceLabel.frame;
        frame.origin.x=cell.productWeightLabel.frame.origin.x+cell.productWeightLabel.frame.size.width+12;
        cell.productPriceLabel.frame=frame;
    }

        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_leftTableView) {
        isLoad=NO;
        leftTableTag=indexPath.row;
       // NSLog(@"gggggggg====%d",indexPath.row);
        imageURL=@"";
        
    _itemCodeStr=[_itemCodeArray objectAtIndex:indexPath.row];
//        for (UIView *v in _rightScrollView.subviews) {
//            if (v.tag==9) {
//                
//                [v removeFromSuperview];
//            }
//            
//        }
        [UIView animateWithDuration:0.5f animations:^
         {
            [_rightScrollView setContentOffset:CGPointMake(0, 0)];
         }completion:^(BOOL finished) {
             
         }];

        

    for (UIView *v in _rightViewContainer.subviews) {
        if (v.tag==9) {
            
            [v removeFromSuperview];
        }
        
    }
        
       
[self requestItemDetail:[_itemCodeArray objectAtIndex:indexPath.row]];
    }
    
}
-(void)shopCartBtnClick
{
    isLoad=NO;
    if ([self.delegate respondsToSelector:@selector(productItemDetailShopCartClick)]) {
        
        [self.delegate productItemDetailShopCartClick];
    }
    
}
-(IBAction)addToShopCartClick:(id)sender
{
    isLoad=NO;
    if ([Global getHomeCustomID]==20||[Global getHomeCustomID]==10) {
        
   
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:_itemCodeStr forKey:@"ItemCode"];
        [params setObject:@"A" forKey:@"EditType"];
        
        //  NSLog(@"addToShopCartParams===%@",params);
        [HttpRequest requestData:@"Guide/EditShopCart" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    // NSLog(@"addToShopCart===%@",severDataDict.dataDict);
//                    if ([self.delegate respondsToSelector:@selector(productItemDetailShopCartClick)]) {
//                        
//                        [self.delegate productItemDetailShopCartClick];
//                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                                             [Global loginViewController:self noteStr:requestResultStr];
                       // [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }
    }
    else if ([Global getHomeCustomID]==35)
    {
       
        if ([Global getLoginToken].length>0) {
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            
            NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
            [params setObject:[Global getLoginToken] forKey:@"Token"];
            [params setObject:_itemCodeStr forKey:@"ItemCode"];
            [params setObject:_companyCodeStr forKey:@"CompanyCode"];
            
            //  NSLog(@"addToShopCartParams===%@",params);
            [HttpRequest requestData:@"Manufacturer/AddShopCart" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        //  [Global showTextHud:requestResultStr view:self.view];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                        // NSLog(@"addToShopCart===%@",severDataDict.dataDict);
                        //                        if ([self.delegate respondsToSelector:@selector(product_SeriesShopCartClick)]) {
                        //
                        //                            [self.delegate product_SeriesShopCartClick];
                        //                        }
                    }
                    else
                    {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        //  [Global showTextHud:requestResultStr view:self.view];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        [Global loginViewController:self noteStr:requestResultStr];
                         //   [Global loginViewController:self];
                        }

                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
            }];
        }

    }

}
-(IBAction)backClick:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(productItemDetailBackClick)]) {
        
        [self.delegate productItemDetailBackClick];
    }

}
 @end
